package com.jbm.jbmsupplier.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JBMhaier on 2017/3/15.
 */

public abstract class BaseAdapter<T> extends android.widget.BaseAdapter{
    protected  List arr;
    protected  Context c;

    public BaseAdapter( Context c) {
        this.c = c;
    }

    /**
     * 更新数据
     * @param arr
     */
    public void setNotify(ArrayList arr){
        if(arr!=null){
            this.arr=arr;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        arr= (List) getArr();
        return arr.size();
    }

    @Override
    public Object getItem(int position) {
        return arr.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     *
     * @param convertView
     * @return
     */
    protected abstract T getHolder(View convertView);
    protected abstract int getLayout();
    protected  abstract T getArr();

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        T  holder= null;
        if (convertView == null) {
            convertView = View.inflate(c,getLayout(), null);
            holder = getHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (T) convertView.getTag();
        }
           initView(holder,position);

        return convertView;
    }
    //初始化数据
    protected abstract void  initView( T holder,int position);
    //初始化监听
    protected  abstract void initListener( T holder,int position);
}
